R语言中的函数6:grep()和grepl(),sub()和gsub()

您所在的位置:网站首页 r语言 fliter R语言中的函数6:grep()和grepl(),sub()和gsub()

R语言中的函数6:grep()和grepl(),sub()和gsub()

2023-09-18 08:18| 来源: 网络整理| 查看: 265

文章目录 `grep()`和`grepl()` 函数介绍用法实例 `sub()`和`gsub()`函数介绍实例

grep()和grepl() 函数介绍

grep()和grepl()是用来查找目标字符串或字符串向量中是否包含目标子串。grep()返回的是整数索引,grepl()返回的是布尔值索引。

用法 grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE, invert = FALSE) grepl(pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

参数介绍:

pattern character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector. Coerced by as.character to a character string if possible. If a character vector of length 2 or more is supplied, the first element is used with a warning. Missing values are allowed except for regexpr, gregexpr and regexec.

x, text a character vector where matches are sought, or an object which can be coerced by as.character to a character vector. Long vectors are supported.

ignore.case if FALSE, the pattern matching is case sensitive and if TRUE, case is ignored during matching.

perl logical. Should Perl-compatible regexps be used?

value if FALSE, a vector containing the (integer) indices of the matches determined by grep is returned, and if TRUE, a vector containing the matching elements themselves is returned.

fixed logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.

useBytes logical. If TRUE the matching is done byte-by-byte rather than character-by-character. See ‘Details’.

invert logical. If TRUE return indices or values for elements that do not match.

实例 #创建一个字符串向量 x gsub(pattern = 'a',replacement = 'QQ',c('a','b','aa')) # [1] "QQ" "b" "QQQQ"


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3